package com.learn.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.shop.constant.NovelContent;
import com.learn.shop.mapper.SDictDataMapper;
import com.learn.shop.pojo.entriy.SDictData;
import com.learn.shop.pojo.entriy.SNovelDetails;
import com.learn.shop.mapper.SNovelDetailsMapper;
import com.learn.shop.pojo.vo.SNovelDetailsVo;
import com.learn.shop.service.SNovelDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

import javax.naming.directory.DirContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小说详情表(SNovelDetails)表服务实现类
 *
 * @author zhengdechang
 * @since 2021-12-20 16:30:42
 */
@Service
@Slf4j
public class SNovelDetailsServiceImpl implements SNovelDetailsService {

    private final Logger logger = LoggerFactory.getLogger(SNovelDetailsServiceImpl.class);

    @Autowired
    private SNovelDetailsMapper sNovelDetailsMapper;

    @Autowired
    private SDictDataMapper sDictDataMapper;

    @Override
    public PageInfo<SNovelDetails> list(SNovelDetailsVo detailsVo) {
        logger.info("小说列表请求参数为-->{}", detailsVo);
        PageHelper.startPage(detailsVo.getPageNum(), detailsVo.getPageSize());
        List<SNovelDetails> list = sNovelDetailsMapper.list(detailsVo);

        List<SDictData> dictList = sDictDataMapper.list(NovelContent.novel_isEnd);
        List<SDictData> typeList = sDictDataMapper.list(NovelContent.novel_type);
        HashMap<String, String> isEndMap = new HashMap<>();
        HashMap<String, String> typeMap = new HashMap<>();
        dictList.forEach(res -> {
            isEndMap.put(res.getDictValue(), res.getDictName());
        });
        typeList.forEach(res -> {
            typeMap.put(res.getDictValue(), res.getDictName());
        });
        list.forEach(dto -> {
            dto.setIsend(isEndMap.get(dto.getIsend()));
            dto.setType(typeMap.get(dto.getType()));
        });
        return new PageInfo<SNovelDetails>(list);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SNovelDetails queryById(Integer id) {
        return this.sNovelDetailsMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sNovelDetails 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<SNovelDetails> queryByPage(SNovelDetails sNovelDetails, PageRequest pageRequest) {
        long total = this.sNovelDetailsMapper.count(sNovelDetails);
        return new PageImpl<>(this.sNovelDetailsMapper.queryAllByLimit(sNovelDetails, pageRequest), pageRequest, total);
    }


    /**
     * 新增数据
     *
     * @param sNovelDetails 实例对象
     * @return 实例对象
     */
    @Override
    public SNovelDetails insert(SNovelDetails sNovelDetails) {
        this.sNovelDetailsMapper.insert(sNovelDetails);
        return sNovelDetails;
    }

    /**
     * 修改数据
     *
     * @param sNovelDetails 实例对象
     * @return 实例对象
     */
    @Override
    public SNovelDetails update(SNovelDetails sNovelDetails) {
        this.sNovelDetailsMapper.update(sNovelDetails);
        return this.queryById(sNovelDetails.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sNovelDetailsMapper.deleteById(id) > 0;
    }
}
